package com.cskaoyan.service.admin.state.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.admin.stat.AdminStatVo;
import com.cskaoyan.bean.vo.admin.stat.goods.AdminStatGoodsVo;
import com.cskaoyan.bean.vo.admin.stat.order.AdminStatOrderVo;
import com.cskaoyan.bean.vo.admin.stat.user.AdminStateUserVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.service.admin.state.AdminStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author MoJunXiang
 * @since 2022/09/07 18:04
 */
@Service
public class AdminStatServiceImpl implements AdminStatService {

    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;


    /**
     * @function_name home
     * @idea 以 *last_login_time* 为依据对所有 **market_user** 分组，返回所有组的 *last_login_time* 和每组 **market_user** 的数量 *users*
     * @return com.cskaoyan.bean.vo.admin.stat.AdminStatVo
     * @author MoJunXiang
     * @since 2022/09/07 20:53
     */
    @Override
    public AdminStatVo user() {
        // 返回值
        AdminStatVo<AdminStateUserVo> stateVo = new AdminStatVo<>();
        ArrayList<AdminStateUserVo> rows = new ArrayList<>();
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("users");
        stateVo.setRows(rows);
        stateVo.setColumns(columns);

        MarketUserExample example = new MarketUserExample();
        List<MarketUser> marketUserList = marketUserMapper.selectByExample(example);

        /* Map对象rowsMap
                key：    day<——>last_login_time
                value：  users<——>last_login_time相同的user数*/
        HashMap<String, Integer> rowsMap = new HashMap<>();
        for (MarketUser marketUser : marketUserList) {
            Date lastLoginTime = marketUser.getLastLoginTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String day = dateFormat.format(lastLoginTime);

            /*如果遍历到的 day 没有 users ，则让 对应day 的 users=1;
            * 若已有 users ，则让 对应day 的 users=users+1 */
            Integer users = rowsMap.get(day);
            if (users == null) {
                rowsMap.put(day, 1);
            } else {
                users++;
                rowsMap.put(day, users);
            }
        }


        Set<String> rowsMapKeySet = rowsMap.keySet();
        for (String day : rowsMapKeySet) {
            Integer users = rowsMap.get(day);
            AdminStateUserVo adminStateUserVo = new AdminStateUserVo(day, users);
            rows.add(adminStateUserVo);
        }

        return stateVo;
    }


    /**
     * @function_name order
     * @idea 以 *add_time* 为依据对所有 **market_order** 分组，返回每组 **market_order** 的统计数据
     * @return com.cskaoyan.bean.vo.admin.stat.AdminStatVo
     * @author MoJunXiang
     * @since 2022/09/07 22:28
     */
    @Override
    public AdminStatVo order() {
        // 返回值
        AdminStatVo<AdminStatOrderVo> statVo = new AdminStatVo<>();
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");
        ArrayList<AdminStatOrderVo> rows = new ArrayList<>();
        statVo.setColumns(columns);
        statVo.setRows(rows);


        List<MarketOrder> marketOrderList = marketOrderMapper.selectByExample(new MarketOrderExample());


        /*处理数据
        * Map对象rowsMap
        *       key：    add_time
        *       value：  Object数组，里面存放两个值
        *                   [0] 为 AdminStatOrderVo对象
        *                   [1] 为 同一个 add_time 的订单的下单顾客的id user_id */
        HashMap<String, Object[]> rowsMap = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (MarketOrder order : marketOrderList) {
            Date addTime = order.getAddTime();
            String day = dateFormat.format(addTime);

            Object[] objects = rowsMap.get(day);
            if (objects == null) {
                AdminStatOrderVo statOrderVo = new AdminStatOrderVo();
                int amount = order.getOrderPrice().intValue();
                statOrderVo.setAmount(amount);
                statOrderVo.setCustomers(1);
                statOrderVo.setDay(day);
                statOrderVo.setPcr(((int) amount / 1));
                statOrderVo.setOrders(1);

                objects = new Object[2];
                objects[0] = statOrderVo;
                objects[1] = order.getUserId();
                rowsMap.put(day, objects);
            } else {
                AdminStatOrderVo statOrderVo = (AdminStatOrderVo) objects[0];
                int amount = statOrderVo.getAmount() + order.getOrderPrice().intValue();
                statOrderVo.setAmount(amount);
                int customers = statOrderVo.getCustomers();
                if (((Integer) objects[1]).intValue() != order.getUserId()) {
                    statOrderVo.setCustomers(customers);
                }
                statOrderVo.setPcr(((int)amount / customers));
                int orders = statOrderVo.getOrders() + 1;
                statOrderVo.setOrders(orders);
            }
        }

        // 将处理完成的 AdminStatOrderVo对象 放入 rows
        Set<String> keySet = rowsMap.keySet();
        for (String day : keySet) {
            Object[] objects = rowsMap.get(day);
            rows.add(((AdminStatOrderVo) objects[0]));
        }


        return statVo;
    }



    /**
     * @function_name goods
     * @idea 以 *add_time* 为依据对所有 **market_order** 分组，返回每组 **market_order** 统计数据。其中包含每组 **market_order** 里所有 **market_order** 所包含的 **market_goods** 的总数
     * @return com.cskaoyan.bean.vo.admin.stat.AdminStatVo
     * @author MoJunXiang
     * @since 2022/09/07 23:49
     */
    @Override
    public AdminStatVo goods() {
        // 返回值
        AdminStatVo<AdminStatGoodsVo> statVo = new AdminStatVo<>();
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");
        ArrayList<AdminStatGoodsVo> rows = new ArrayList<>();
        statVo.setColumns(columns);
        statVo.setRows(rows);


        List<MarketOrder> marketOrderList = marketOrderMapper.selectByExample(new MarketOrderExample());
        List<MarketOrderGoods> marketOrderGoodList = marketOrderGoodsMapper.selectByExample(new MarketOrderGoodsExample());


        /*TODO:
        *   遍历 marketOrderList ，按照 add_time 分类，用 rowsMap 来分类
        *       遍历 marketOrderGoodList ，进行相关操作
    *        Map对象rowsMap
    *           key：    day<——> markett_order 的 add_time
    *                    value<——> AdminStatGoodsVo对象*/
        HashMap<String, AdminStatGoodsVo> rowsMap = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (MarketOrder order : marketOrderList) {
            Date addTime = order.getAddTime();
            String day = dateFormat.format(addTime);

            AdminStatGoodsVo statGoodsVo = rowsMap.get(day);
            if (statGoodsVo == null) {
                statGoodsVo = new AdminStatGoodsVo();
                statGoodsVo.setAmount(order.getOrderPrice().intValue());
                statGoodsVo.setDay(day);
                statGoodsVo.setOrders(1);

            } else {
                int amount = statGoodsVo.getAmount();
                statGoodsVo.setAmount(amount + order.getOrderPrice().intValue());
                int orders = statGoodsVo.getOrders();
                statGoodsVo.setOrders(orders + 1);
            }
            for (MarketOrderGoods orderGoods : marketOrderGoodList) {
                // 如果 订单order 里的 商品id<-->goodsId 等于 商品orderGoods 里的 id
                if (order.getId().intValue() == orderGoods.getOrderId().intValue()) {
                    // 则将当前 order 里的 商品数 加入到 AdminStatGoodsVo对象statGoodsVo 中
                    int products = statGoodsVo.getProducts();
                    statGoodsVo.setProducts(products + orderGoods.getNumber());
                }
            }
            // 将更新完毕的 AdminStatGoodsVo对象statGoodsVo 放入 rowsMap 中
            rowsMap.put(day, statGoodsVo);
        }



        /*将转换后的所有 AdminStatGoodsVo对象statGoodsVo 放入 rows 中*/
        Set<String> keySet = rowsMap.keySet();
        for (String day : keySet) {
            AdminStatGoodsVo statGoodsVo = rowsMap.get(day);
            rows.add(statGoodsVo);
        }


        return statVo;
    }


}
